﻿using System;
using System.ComponentModel;
using NUnit.Framework;
using Present.Commands.Async.Actions.Decorators;
using Rhino.Mocks;

namespace Present.Commands.Tests.Decorators
{
    [TestFixture]
    public class BusyAsyncActionDecoratorTestCase : AsyncActionDecoratorTestCaseBase<BusyAsyncActionDecorator<int, int>, int, int>
    {
        private IBusyable busyable;

        public override void Setup()
        {
            busyable = MockRepository.GenerateStub<IBusyable>();
            base.Setup();
        }

        [Test]
        public void BusyIsSetOnStart()
        {
            Decorator.PrepareWork(1);
            Assert.IsTrue( busyable.IsBusy);
        }

        [Test]
        public void BusyIsUnSetOnError()
        {
            busyable.IsBusy = true;
            Decorator.HandleError(new Exception());
            Assert.IsFalse(busyable.IsBusy);
        }

        [Test]
        public void BusyIsUnsertOnSuccess()
        {
            busyable.IsBusy = true;
            Decorator.HandleSuccess(1);
            Assert.IsFalse(busyable.IsBusy);
        }

        [Test]
        public void CanExecuteChangedRaisedWhenIsBusyChanges()
        {
            bool fired = false;
            Decorator.CanExecuteChanged += (sender, e) => fired = true;
            busyable.Raise(x => x.PropertyChanged += null, busyable, new PropertyChangedEventArgs("IsBusy"));
            Assert.IsTrue(fired);
        }


        protected override BusyAsyncActionDecorator<int, int> CreateDecorator()
        {
           return new BusyAsyncActionDecorator<int, int>(Inner, busyable);
        }

        protected override int GetParameter()
        {
            return 1;
        }

        protected override int GetResult()
        {
            return 1;
        }
    }
}